home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Shells / zsh / Source / src / zle_word.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-07  |  6.9 KB  |  396 lines

  1.  
  2. /*
  3.  *
  4.  * zle_word.c - word-related editor functions
  5.  *
  6.  * This file is part of zsh, the Z shell.
  7.  *
  8.  * This software is Copyright 1992 by Paul Falstad
  9.  *
  10.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  11.  * use this software as long as: there is no monetary profit gained
  12.  * specifically from the use or reproduction of this software, it is not
  13.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  14.  * included prominently in any copy made.
  15.  *
  16.  * The author make no claims as to the fitness or correctness of this software
  17.  * for any use whatsoever, and it is provided as is. Any use of this software
  18.  * is at the user's own risk.
  19.  *
  20.  */
  21.  
  22. #define ZLE
  23. #include "zsh.h"
  24.  
  25. void forwardword()
  26. {                /**/
  27.     if (mult < 0) {
  28.     mult = -mult;
  29.     backwardword();
  30.     return;
  31.     }
  32.     while (mult--) {
  33.     while (cs != ll && iword(line[cs]))
  34.         cs++;
  35.     if (wordflag && !mult)
  36.         return;
  37.     while (cs != ll && !iword(line[cs]))
  38.         cs++;
  39.     }
  40. }
  41.  
  42. void viforwardword()
  43. {                /**/
  44.     if (mult < 0) {
  45.     mult = -mult;
  46.     backwardword();
  47.     return;
  48.     }
  49.     while (mult--) {
  50.     if (iident(line[cs]))
  51.         while (cs != ll && iident(line[cs]))
  52.         cs++;
  53.     else
  54.         while (cs != ll && !iident(line[cs]) && !iblank(line[cs]))
  55.         cs++;
  56.     if (wordflag && !mult)
  57.         return;
  58.     while (cs != ll && iblank(line[cs]))
  59.         cs++;
  60.     }
  61. }
  62.  
  63. void viforwardblankword()
  64. {                /**/
  65.     if (mult < 0) {
  66.     mult = -mult;
  67.     vibackwardblankword();
  68.     return;
  69.     }
  70.     while (mult--) {
  71.     while (cs != ll && !iblank(line[cs]))
  72.         cs++;
  73.     if (wordflag && !mult)
  74.         return;
  75.     while (cs != ll && iblank(line[cs]))
  76.         cs++;
  77.     }
  78. }
  79.  
  80. void emacsforwardword()
  81. {                /**/
  82.     if (mult < 0) {
  83.     mult = -mult;
  84.     emacsbackwardword();
  85.     return;
  86.     }
  87.     while (mult--) {
  88.     while (cs != ll && !iword(line[cs]))
  89.         cs++;
  90.     if (wordflag && !mult)
  91.         return;
  92.     while (cs != ll && iword(line[cs]))
  93.         cs++;
  94.     }
  95. }
  96.  
  97. void viforwardblankwordend()
  98. {                /**/
  99.     if (mult < 0)
  100.     return;
  101.     while (mult--) {
  102.     while (cs != ll && iblank(line[cs + 1]))
  103.         cs++;
  104.     while (cs != ll && !iblank(line[cs + 1]))
  105.         cs++;
  106.     }
  107.     if (cs != ll && virangeflag)
  108.     cs++;
  109. }
  110.  
  111. void viforwardwordend()
  112. {                /**/
  113.     if (mult < 0) {
  114.     mult = -mult;
  115.     backwardword();
  116.     return;
  117.     }
  118.     while (mult--) {
  119.     if (iblank(line[cs + 1]))
  120.         while (cs != ll && iblank(line[cs + 1]))
  121.         cs++;
  122.     if (iident(line[cs + 1]))
  123.         while (cs != ll && iident(line[cs + 1]))
  124.         cs++;
  125.     else
  126.         while (cs != ll && !iident(line[cs + 1]) && !iblank(line[cs + 1]))
  127.         cs++;
  128.     }
  129.     if (cs != ll && virangeflag)
  130.     cs++;
  131. }
  132.  
  133. void backwardword()
  134. {                /**/
  135.     if (mult < 0) {
  136.     mult = -mult;
  137.     forwardword();
  138.     return;
  139.     }
  140.     while (mult--) {
  141.     while (cs && !iword(line[cs - 1]))
  142.         cs--;
  143.     while (cs && iword(line[cs - 1]))
  144.         cs--;
  145.     }
  146. }
  147.  
  148. void vibackwardword()
  149. {                /**/
  150.     if (mult < 0) {
  151.     mult = -mult;
  152.     backwardword();
  153.     return;
  154.     }
  155.     while (mult--) {
  156.     while (cs && iblank(line[cs - 1]))
  157.         cs--;
  158.     if (iident(line[cs - 1]))
  159.         while (cs && iident(line[cs - 1]))
  160.         cs--;
  161.     else
  162.         while (cs && !iident(line[cs - 1]) && !iblank(line[cs - 1]))
  163.         cs--;
  164.     }
  165. }
  166.  
  167. void vibackwardblankword()
  168. {                /**/
  169.     if (mult < 0) {
  170.     mult = -mult;
  171.     viforwardblankword();
  172.     return;
  173.     }
  174.     while (mult--) {
  175.     while (cs && iblank(line[cs - 1]))
  176.         cs--;
  177.     while (cs && !iblank(line[cs - 1]))
  178.         cs--;
  179.     }
  180. }
  181.  
  182. void emacsbackwardword()
  183. {                /**/
  184.     if (mult < 0) {
  185.     mult = -mult;
  186.     emacsforwardword();
  187.     return;
  188.     }
  189.     while (mult--) {
  190.     while (cs && !iword(line[cs - 1]))
  191.         cs--;
  192.     while (cs && iword(line[cs - 1]))
  193.         cs--;
  194.     }
  195. }
  196.  
  197. void backwarddeleteword()
  198. {                /**/
  199.     int x = cs;
  200.  
  201.     if (mult < 0) {
  202.     mult = -mult;
  203.     deleteword();
  204.     return;
  205.     }
  206.     while (mult--) {
  207.     while (x && !iword(line[x - 1]))
  208.         x--;
  209.     while (x && iword(line[x - 1]))
  210.         x--;
  211.     }
  212.     backdel(cs - x);
  213. }
  214.  
  215. void vibackwardkillword()
  216. {                /**/
  217.     int x = cs;
  218.  
  219.     if (mult < 0) {
  220.     feep();
  221.     return;
  222.     }
  223. /* this taken from "vibackwardword" */
  224.     while (mult--) {
  225.     while ((x > viinsbegin) && iblank(line[x - 1]))
  226.         x--;
  227.     if (iident(line[x - 1]))
  228.         while ((x > viinsbegin) && iident(line[x - 1]))
  229.         x--;
  230.     else
  231.         while ((x > viinsbegin) && !iident(line[x - 1]) && !iblank(line[x - 1]))
  232.         x--;
  233.     }
  234. /*
  235.         while (mult--) {
  236.                 while ( (x > viinsbegin) && (iwordsep(line[x-1]))) x--;
  237.                 while ( (x > viinsbegin) && (!iwordsep(line[x-1]))) x--;
  238.         }
  239.         */
  240.     backkill(cs - x, 1);
  241. }
  242.  
  243. void backwardkillword()
  244. {                /**/
  245.     int x = cs;
  246.  
  247.     if (mult < 0) {
  248.     mult = -mult;
  249.     killword();
  250.     return;
  251.     }
  252.     while (mult--) {
  253.     while (x && !iword(line[x - 1]))
  254.         x--;
  255.     while (x && iword(line[x - 1]))
  256.         x--;
  257.     }
  258.     backkill(cs - x, 1);
  259. }
  260.  
  261. void upcaseword()
  262. {                /**/
  263.     int neg = mult < 0, ocs = cs;
  264.  
  265.     if (neg)
  266.     mult = -mult;
  267.     while (mult--) {
  268.     while (cs != ll && !iword(line[cs]))
  269.         cs++;
  270.     while (cs != ll && iword(line[cs])) {
  271.         line[cs] = tuupper(line[cs]);
  272.         cs++;
  273.     }
  274.     }
  275.     if (neg)
  276.     cs = ocs;
  277. }
  278.  
  279. void downcaseword()
  280. {                /**/
  281.     int neg = mult < 0, ocs = cs;
  282.  
  283.     if (neg)
  284.     mult = -mult;
  285.     while (mult--) {
  286.     while (cs != ll && !iword(line[cs]))
  287.         cs++;
  288.     while (cs != ll && iword(line[cs])) {
  289.         line[cs] = tulower(line[cs]);
  290.         cs++;
  291.     }
  292.     }
  293.     if (neg)
  294.     cs = ocs;
  295. }
  296.  
  297. void capitalizeword()
  298. {                /**/
  299.     int first;
  300.     int neg = mult < 0, ocs = cs;
  301.  
  302.     if (neg)
  303.     mult = -mult;
  304.     while (mult--) {
  305.     first = 1;
  306.     while (cs != ll && !iword(line[cs]))
  307.         cs++;
  308.     while (cs != ll && iword(line[cs])) {
  309.         line[cs] = (first) ? tuupper(line[cs]) : tulower(line[cs]);
  310.         first = 0;
  311.         cs++;
  312.     }
  313.     }
  314.     if (neg)
  315.     cs = ocs;
  316. }
  317.  
  318. void deleteword()
  319. {                /**/
  320.     int x = cs;
  321.  
  322.     if (mult < 0) {
  323.     mult = -mult;
  324.     backwarddeleteword();
  325.     return;
  326.     }
  327.     while (mult--) {
  328.     while (x != ll && !iword(line[x]))
  329.         x++;
  330.     while (x != ll && iword(line[x]))
  331.         x++;
  332.     }
  333.     foredel(x - cs);
  334. }
  335.  
  336. void killword()
  337. {                /**/
  338.     int x = cs;
  339.  
  340.     if (mult < 0) {
  341.     mult = -mult;
  342.     backwardkillword();
  343.     return;
  344.     }
  345.     while (mult--) {
  346.     while (x != ll && !iword(line[x]))
  347.         x++;
  348.     while (x != ll && iword(line[x]))
  349.         x++;
  350.     }
  351.     forekill(x - cs, 0);
  352. }
  353.  
  354. void transposewords()
  355. {                /**/
  356.     int p1, p2, p3, p4, x = cs;
  357.     char *temp, *pp;
  358.     int neg = mult < 0, ocs = cs;
  359.  
  360.     if (neg)
  361.     mult = -mult;
  362.     while (mult--) {
  363.     while (x != ll && line[x] != '\n' && !iword(line[x]))
  364.         x++;
  365.     if (x == ll || line[x] == '\n') {
  366.         x = cs;
  367.         while (x && line[x - 1] != '\n' && !iword(line[x]))
  368.         x--;
  369.         if (!x || line[x - 1] == '\n') {
  370.         feep();
  371.         return;
  372.         }
  373.     }
  374.     for (p4 = x; p4 != ll && iword(line[p4]); p4++);
  375.     for (p3 = p4; p3 && iword(line[p3 - 1]); p3--);
  376.     if (!p3) {
  377.         feep();
  378.         return;
  379.     }
  380.     for (p2 = p3; p2 && !iword(line[p2 - 1]); p2--);
  381.     if (!p2) {
  382.         feep();
  383.         return;
  384.     }
  385.     for (p1 = p2; p1 && iword(line[p1 - 1]); p1--);
  386.     pp = temp = (char *)halloc(p4 - p1 + 1);
  387.     struncpy(&pp, UTOSCP(line + p3), p4 - p3);
  388.     struncpy(&pp, UTOSCP(line + p2), p3 - p2);
  389.     struncpy(&pp, UTOSCP(line + p1), p2 - p1);
  390.     strncpy((char *)line + p1, temp, p4 - p1);
  391.     cs = p4;
  392.     }
  393.     if (neg)
  394.     cs = ocs;
  395. }
  396.